package com.jniu.customviewprogram.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jniu.customviewprogram.R;

import java.util.zip.Inflater;

/**
 * 仿Ios弹出框
 * Created by jniu on 2017/8/22.
 */

public class IosAlertDialog {

    private Context context;
    private LayoutInflater inflater;
    private final Display display;//屏幕尺寸

    private LinearLayout rootView;
    private TextView tvTitle;
    private TextView tvMsg;
    private View msgLine;
    private View btnLine;
    private Button btnCancel;
    private Button btnOk;

    private Dialog dialog;

    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showMsgLine = false;
    private boolean showBtnLine = false;
    private boolean showCancel = false;
    private boolean showOk = false;

    public IosAlertDialog(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public IosAlertDialog builder(){
        View view = inflater.inflate(R.layout.dialog_ios,null);

        rootView = (LinearLayout) view.findViewById(R.id.layout_root);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvTitle.setVisibility(View.GONE);
        tvMsg = (TextView) view.findViewById(R.id.tv_msg);
        tvMsg.setVisibility(View.GONE);
        msgLine = view.findViewById(R.id.view_msg_line);
        msgLine.setVisibility(View.GONE);
        btnLine =  view.findViewById(R.id.view_btn_line);
        btnLine.setVisibility(View.GONE);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnCancel.setVisibility(View.GONE);
        btnOk = (Button) view.findViewById(R.id.btn_ok);
        btnOk.setVisibility(View.GONE);

        dialog = new Dialog(context,R.style.IosAlertDialog);
        dialog.setContentView(view);

        rootView.setLayoutParams(new FrameLayout.LayoutParams((int)(display.getWidth()*0.85),//dialog宽度占屏幕占比
                ViewGroup.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public IosAlertDialog setTitle(String title){
        showTitle = true;
        if (TextUtils.isDigitsOnly(title)) {
            tvTitle.setText("标题");
        }else{
            tvTitle.setText(title);
        }

        return this;
    }

    public IosAlertDialog setMsg(String msg){
        showMsg = true;
        if (TextUtils.isDigitsOnly(msg)) {
            tvMsg.setText("内容");
        }else{
            tvMsg.setText(msg);
        }

        return this;
    }

    public IosAlertDialog setCancelButton(String text, final View.OnClickListener listener){
        showCancel = true;
        if (TextUtils.isEmpty(text)) {
            btnCancel.setText("取消");
        }else {
            btnCancel.setText(text);
        }
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

    public IosAlertDialog setOkButton(String text, final View.OnClickListener listener){
        showOk = true;
        if (TextUtils.isEmpty(text)) {
            btnOk.setText("确定");
        }else {
            btnOk.setText(text);
        }
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

    public IosAlertDialog show(){
        setLayout();
        dialog.show();
        return this;
    }

    private void setLayout() {
        if (!showTitle && !showMsg) {
            tvTitle.setText("提示");
            tvTitle.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            tvTitle.setVisibility(View.VISIBLE);
        }

        if (showMsg) {
            tvMsg.setVisibility(View.VISIBLE);
        }

        if (showCancel) {
            btnCancel.setVisibility(View.VISIBLE);
        }

        if (showOk) {
            btnOk.setVisibility(View.VISIBLE);
        }

        if (showCancel && showOk) {
            btnLine.setVisibility(View.VISIBLE);
        }
    }
}
